package com.demoo.zjsu.util.HttpClientUtil.pool;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ResourceBundle;

/**
 * Created by jshjsh on 2016/9/8.
 */
public class DownLoad {

    private static ResourceBundle bundle = ResourceBundle.getBundle("httpclient");
    private static final String maxTotal;
    private static final String connectionRequestTimeout;
    private static final String connectTimeout;
    private static final String socketTimeout;
    private static final String defaultMaxPerRoute;

    static {
        maxTotal = bundle.getString("maxTotal");
        connectionRequestTimeout = bundle.getString("connectionRequestTimeout");
        connectTimeout = bundle.getString("connectTimeout");
        socketTimeout = bundle.getString("socketTimeout");
        defaultMaxPerRoute = bundle.getString("defaultMaxPerRoute");
    }


    public static PoolingHttpClientConnectionManager getManager() {
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
        manager.setDefaultMaxPerRoute(Integer.valueOf(defaultMaxPerRoute));
        manager.setMaxTotal(Integer.valueOf(maxTotal));
        return manager;
    }

    public static RequestConfig getRequestConfig() {
        return RequestConfig.custom().setConnectionRequestTimeout(Integer.valueOf(connectionRequestTimeout))
                .setConnectTimeout(Integer.valueOf(connectTimeout))
                .setSocketTimeout(Integer.valueOf(socketTimeout)).build();
    }

    public static CloseableHttpClient getClient() {
        return HttpClientBuilder.create().setConnectionManager(getManager()).setRetryHandler(new AgainConfig())
                .setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)\n" +
                "         Chrome/52.0.2743.82 Safari/537.36").build();
    }

    private static IdleConnectionEvictor evictor = new IdleConnectionEvictor(getManager(),120000);

    public static void download(String url, String path, String filename) {
        HttpGet get = new HttpGet(url);
        CloseableHttpClient client = getClient();
        get.setConfig(getRequestConfig());
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                FileOutputStream os = null;
                try {
                    File file = new File(path);
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    os = new FileOutputStream(path + filename);
                    InputStream is = entity.getContent();
                    IOUtils.copy(is, os);
                    EntityUtils.consume(entity);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    HttpClientUtils.closeQuietly(response);
                    HttpClientUtils.closeQuietly(client);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
